package com.appheader.rongmaphone.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.appheader.rongmaphone.R;
import com.appheader.rongmaphone.utils.network.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SuggestActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout suggestBack;
    private EditText suggestEditText;
    private Button suggestBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_suggest);
        initView();
        setOnClickListener();
    }

    private void setOnClickListener() {
        suggestBack.setOnClickListener(this);
        suggestBtn.setOnClickListener(this);
    }

    private void initView() {
        suggestBack = (LinearLayout) findViewById(R.id.suggest_back);
        suggestEditText = (EditText) findViewById(R.id.help_feedback);
        suggestBtn = (Button) findViewById(R.id.but_help_feedback);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.suggest_back://返回监听
                finish();
                break;
            case R.id.but_help_feedback://提交意见
                String suggest = suggestEditText.getText().toString().trim();
                //post提交到服务器
//                if (TextUtils.isEmpty(suggest)) {
//                    Toast.makeText(SuggestActivity.this, "意见提交不能为空", Toast.LENGTH_SHORT).show();
//                } else {

                HttpUtil.getCloudSuggestOkHttpClient(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Log.e("SuggestActivity", "onResponse: --" + result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String success = jsonObject.getString("success");
                            String errorMsg = jsonObject.getString("errorMsg");
                            if (success.equals("false")) {
                                Toast.makeText(SuggestActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

                            } else {
                                Toast.makeText(SuggestActivity.this, "提交成功！", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, suggest);
//                }
                break;
        }
    }
}
